package com.shankungfu.main;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.shankungfu.GlobleConstant;
import com.shankungfu.MainApplication;
import com.shankungfu.R;
import com.shankungfu.bean.PickItemBean;
import com.shankungfu.bean.UserBean;
import com.shankungfu.fragment.MyMenuFragment;
import com.shankungfu.util.MyAsyncHttpResponseHandler;
import com.shankungfu.util.TestUtils;
import com.shankungfu.util.ToastUtil;
import com.shankungfu.util.WebService;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LogInActivity extends Activity implements OnClickListener {

	private TextView forget_Password;
	private EditText username;
	private EditText password;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.log_in_layout);

		forget_Password = (TextView) findViewById(R.id.forget_password);
		
		
		username = (EditText) findViewById(R.id.username_edit);
		password = (EditText) findViewById(R.id.password_edit);
		forget_Password.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
		forget_Password.setOnClickListener(this);

		Button bt = (Button) findViewById(R.id.goback);
		bt.setOnClickListener(this);

	}
	
	

	@Override
	public void onClick(View v) {
		String usernameStr = username.getText().toString();
		String passwordStr = password.getText().toString();

		switch (v.getId()) {
		case R.id.goback:
			
			setResult(CommentActivity.DENGLU_SUCCESS_CODE);
			finish();
			break;
		case R.id.register:
			Intent intentLogin = new Intent(this, RegisterActivity.class);
			startActivity(intentLogin);
			break;
		case R.id.forget_password:
			Intent intent = new Intent(this, WebViewActivity.class);
			startActivity(intent);
			break;
		case R.id.logIn:
			if (!TextUtils.isEmpty(usernameStr) && !TextUtils.isEmpty(passwordStr)) {
				getUserData(usernameStr, passwordStr);
			} else {
				ToastUtil.showMessageText(this, "用户名或密码不能为空");
			}
			
			break;
		}

	}


	private void getUserData(String usernameStr, String passwordStr) {
		WebService httpclient = new WebService();
		RequestParams params = new RequestParams();
		params.add("serviceId", "login");
		params.add("user_name", usernameStr);
		params.add("password", passwordStr);
		AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {
			@Override
			public void onSuccess(String content) {
				TestUtils.logI("LogInActivity content " +content);
				if (!TextUtils.isEmpty(content)
						&& content.contains("\"result\":0")) {
					UserBean userBean = JSON.parseObject(content,
							UserBean.class);
					MainApplication.setUserIsLogin(true);
					MainApplication.userBean = userBean;
					ToastUtil.showMessageText(LogInActivity.this, "登录成功");
					setResult(PickItemDetailActivity.SUCCESS_CODE);
					setResult(CommentActivity.DENGLU_SUCCESS_CODE);
					
					finish();
				} else {
					UserBean userBean = JSON.parseObject(content,
							UserBean.class);
					ToastUtil.showMessageText(LogInActivity.this, userBean.getMsg());
				}
			}

			@Override
			public void onFailure(Throwable error, String content) {
				try {
					if (!TextUtils.isEmpty(content)) {
						UserBean userBean = JSON.parseObject(content,
								UserBean.class);
						if (userBean != null && TextUtils.isEmpty(userBean.getMsg())) {
							ToastUtil.showMessageText(LogInActivity.this, userBean.getMsg());
							return;
						}
					}
				} catch (Exception e) {
					ToastUtil.showMessageText(LogInActivity.this, "登录失败,请重新尝试");
					e.printStackTrace();
				}
				ToastUtil.showMessageText(LogInActivity.this, "登录失败,请重新尝试");
			}
		};
		httpclient
				.request(GlobleConstant.LOGIN_PROCESS, params, responseHandler);

	}

}
